package com.fqgj.sentry.biz.antiFraud;

import com.fqgj.report.statistics.entity.RiskBiGpsStatEntity;
import com.fqgj.sentry.common.utils.Arith;
import com.fqgj.sentry.controller.antiFraud.request.GpsDistributeQuery;
import com.fqgj.sentry.controller.antiFraud.response.gps.GpsElement;
import com.fqgj.sentry.controller.antiFraud.response.gps.GpsElementData;
import com.fqgj.sentry.controller.antiFraud.response.gps.GpsElementDateDetail;
import com.fqgj.sentry.controller.antiFraud.response.gps.GpsInfoData;
import com.fqgj.sentry.statistics.enums.GpsQueryTypeEnums;
import com.fqgj.sentry.statistics.service.GpsStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class GpsDistributeBusiness {

    @Autowired
    private GpsStatisticsService gpsStatisticsService;


    public GpsInfoData getGpsInfoData(GpsDistributeQuery gpsDistributeQuery) {
        GpsInfoData gpsInfoData = new GpsInfoData();
        //gps 省份集合
        List<GpsElement> provinceList = new ArrayList<>();
        List<RiskBiGpsStatEntity> provinceGpsList = gpsStatisticsService.selectGpsList(gpsDistributeQuery.getSource()
                , gpsDistributeQuery.getIsNew(), gpsDistributeQuery.getStartDate(),
                gpsDistributeQuery.getEndDate(), GpsQueryTypeEnums.province);
        //申请量分布
        Integer maxApplyNum = 0;
        GpsElement applyElement = new GpsElement("申请量分布");
        List<GpsElementData> applyElementDataList = new ArrayList<>();
        for (RiskBiGpsStatEntity province : provinceGpsList) {
            if (province.getApplyCnt() > maxApplyNum) {
                maxApplyNum = province.getApplyCnt();
            }
            applyElementDataList.add(new GpsElementData(province.getProvince(),
                    province.getApplyCnt(), getGpsElementDateDesc(province)));
        }
        applyElement.setMaxValue(maxApplyNum).setData(applyElementDataList);
        provinceList.add(applyElement);

        //首逾分布率
        Double oneDayOverdueMaxNum = 0d;
        GpsElement oneDayOverdue = new GpsElement("首逾分布率");
        List<GpsElementData> oneDayOverdueList = new ArrayList<>();
        for (RiskBiGpsStatEntity province : provinceGpsList) {

            Integer oneDayOverdueNum = province.getD0Cnt();
            Integer oneDayShould = province.getD0ShouldRepayCnt();
            Double oneDayRateDouble = oneDayShould == 0 ? 0d : Arith.multiply(Arith.divide(oneDayOverdueNum, oneDayShould, 2), 100);
            if (oneDayRateDouble > oneDayOverdueMaxNum) {
                oneDayOverdueMaxNum = oneDayRateDouble;
            }
            oneDayOverdueList.add(new GpsElementData(province.getProvince(),
                    oneDayRateDouble.intValue(), getGpsElementDateDesc(province)));
        }
        oneDayOverdue.setMaxValue((int) (Math.ceil(oneDayOverdueMaxNum))).setData(oneDayOverdueList);
        provinceList.add(oneDayOverdue);

        //d15分布率
        Double fifteenDayOverdueMaxNum = 0d;
        GpsElement fifteenOverdue = new GpsElement("d15分布率");
        List<GpsElementData> fifteenOverdueList = new ArrayList<>();
        for (RiskBiGpsStatEntity province : provinceGpsList) {
            Integer fifteenShould = province.getD15ShouldRepayCnt();
            Integer fifteenOverdueNum = province.getD15Cnt();
            Double fifteenRate = fifteenShould == 0 ? 0d : Arith.multiply(Arith.divide(fifteenOverdueNum, fifteenShould, 2), 100);
            if (fifteenRate > fifteenDayOverdueMaxNum) {
                fifteenDayOverdueMaxNum = fifteenRate;
            }

            fifteenOverdueList.add(new GpsElementData(province.getProvince(),
                    fifteenRate.intValue(), getGpsElementDateDesc(province)));
        }
        fifteenOverdue.setMaxValue((int) (Math.ceil(fifteenDayOverdueMaxNum))).setData(fifteenOverdueList);
        provinceList.add(fifteenOverdue);


        //gps 城市集合
        List<GpsElement> area = new ArrayList<>();
        Map<String, Double[]> geoCoordMap = new HashMap<>();
        List<RiskBiGpsStatEntity> cityGpsList = gpsStatisticsService.selectGpsList(gpsDistributeQuery.getSource()
                , gpsDistributeQuery.getIsNew(), gpsDistributeQuery.getStartDate(),
                gpsDistributeQuery.getEndDate(), GpsQueryTypeEnums.city);
        //申请量分布
        Integer areaMaxApplyNum = 0;
        GpsElement areaApplyElement = new GpsElement("申请量分布");
        List<GpsElementData> areaApplyElementDataList = new ArrayList<>();
        for (RiskBiGpsStatEntity city : cityGpsList) {
            if (city.getGpsLat() == null
                    || city.getGpsLng() == null) {
                continue;
            }
            if (city.getApplyCnt() > areaMaxApplyNum) {
                areaMaxApplyNum = city.getApplyCnt();
            }
            areaApplyElementDataList.add(new GpsElementData(city.getCity(),
                    city.getApplyCnt(), getGpsElementDateDesc(city)));
            geoCoordMap.put(city.getCity(), getDoubleArray(city.getGpsLat().toString(), city.getGpsLng().toString()));
        }
        areaApplyElement.setMaxValue(areaMaxApplyNum).setData(areaApplyElementDataList);
        area.add(areaApplyElement);

        //首逾分布率
        Double areaOneDayOverdueMaxNum = 0d;
        GpsElement areaOneDayOverdue = new GpsElement("首逾分布率");
        List<GpsElementData> areaOneDayOverdueList = new ArrayList<>();
        for (RiskBiGpsStatEntity city : cityGpsList) {
            if (city.getGpsLat() == null
                    || city.getGpsLng() == null) {
                continue;
            }
            Integer oneDayOverdueNum = city.getD0Cnt();
            Integer oneDayShould = city.getD0ShouldRepayCnt();
            Double oneDayRate = oneDayShould == 0 ? 0d : Arith.multiply(Arith.divide(oneDayOverdueNum, oneDayShould, 2), 100);

            if (oneDayRate > areaOneDayOverdueMaxNum) {
                areaOneDayOverdueMaxNum = oneDayRate;
            }
            areaOneDayOverdueList.add(new GpsElementData(city.getCity(),
                    oneDayRate.intValue(), getGpsElementDateDesc(city)));
            geoCoordMap.put(city.getCity(), getDoubleArray(city.getGpsLat().toString(), city.getGpsLng().toString()));
        }
        areaOneDayOverdue.setMaxValue((int) (Math.ceil(areaOneDayOverdueMaxNum))).setData(areaOneDayOverdueList);
        area.add(areaOneDayOverdue);

        //d15分布率
        Double areaFifteenDayOverdueMaxNum = 0d;
        GpsElement areaFifteenOverdue = new GpsElement("d15分布率");
        List<GpsElementData> areaFifteenOverdueList = new ArrayList<>();
        for (RiskBiGpsStatEntity city : cityGpsList) {

            Integer fifteenShould = city.getD15ShouldRepayCnt();
            Integer fifteenOverdueNum = city.getD15Cnt();
            Double fifteenRate = fifteenShould == 0 ? 0d : Arith.multiply(Arith.divide(fifteenOverdueNum, fifteenShould, 2), 100);
            if (city.getGpsLat() == null
                    || city.getGpsLng() == null) {
                continue;
            }
            if (fifteenRate > areaFifteenDayOverdueMaxNum) {
                areaFifteenDayOverdueMaxNum = fifteenRate;
            }

            areaFifteenOverdueList.add(new GpsElementData(city.getProvince(),
                    fifteenRate.intValue(), getGpsElementDateDesc(city)));

            geoCoordMap.put(city.getCity(), getDoubleArray(city.getGpsLat().toString(), city.getGpsLng().toString()));
        }
        areaFifteenOverdue.setMaxValue((int) (Math.ceil(areaFifteenDayOverdueMaxNum))).setData(areaFifteenOverdueList);
        area.add(areaFifteenOverdue);

        gpsInfoData.setProvinceList(provinceList).setAreaList(area);
        gpsInfoData.setGeoCoordMap(geoCoordMap);

        return gpsInfoData;
    }

    private static Double[] getDoubleArray(String lat, String lng) {
        Double[] strArray = {Double.valueOf(lat), Double.valueOf(lng)};
        return strArray;
    }

    private static GpsElementDateDetail getGpsElementDateDesc(RiskBiGpsStatEntity riskBiGpsStatEntity) {

        Integer passNum = riskBiGpsStatEntity.getPaidCnt();
        Integer applyNum = riskBiGpsStatEntity.getApplyCnt();
        Integer oneDayOverdue = riskBiGpsStatEntity.getD0Cnt();
        Integer oneDayShould = riskBiGpsStatEntity.getD0ShouldRepayCnt();
        Integer fifteenShould = riskBiGpsStatEntity.getD15ShouldRepayCnt();
        Integer fifteenOverdue = riskBiGpsStatEntity.getD15Cnt();
        Integer onOverdue = riskBiGpsStatEntity.getOverdueCnt();
        GpsElementDateDetail gpsElementDateDetail = new GpsElementDateDetail();
        gpsElementDateDetail.setApplyNum(applyNum)
                .setPassNum(passNum)
                .setPassRate(applyNum == 0 ? "暂无数据" : Arith.multiply(Arith.divide(passNum, applyNum, 4), 100) + "%")
                .setOneDayShouldPayNum(oneDayShould)
                .setOneDayOverdueNum(oneDayOverdue)
                .setOneDayOverdueRate(oneDayShould == 0 ? "暂无数据" : Arith.multiply(Arith.divide(oneDayOverdue, oneDayShould, 4), 100) + "")
                .setFifteenShouldPayNum(fifteenShould)
                .setFifteenOverdueNum(fifteenOverdue)
                .setFifteenOverdueRate(fifteenShould == 0 ? "暂无数据" : Arith.multiply(Arith.divide(fifteenOverdue, fifteenShould, 4), 100) + "%")
                .setOnOverdueNum(onOverdue)
                .setOnOverdueRate(oneDayShould == 0 ? "暂无数据" : Arith.multiply(Arith.divide(onOverdue, oneDayShould, 4), 100) + "%");
        return gpsElementDateDetail;

    }


    public static void main(String[] args) {

        System.out.println((int) (Math.ceil(12.11d)));
    }
}
